/**
 * 底层函数的实现
 * functions.c
 */

#include "functions.h"



Club *head = NULL;




void swap_str(char *dest, const char *src)
{
 	char *temp = (char *)malloc((strlen(dest) + 1) * sizeof(char));
    strcpy(temp, dest);
    dest = (char *)realloc(dest, (strlen(temp) + strlen(src) + 1) * sizeof(char));
    strcpy(dest, temp);
    strcat(dest, src);
    free(temp);
    temp = NULL;                                                                                                                       
    
    debug("length: %d", strlen(dest));

    return;
}


char * tag_to_string(char tag)
{
	switch(tag)
	{
		case '0':
			return "主将局";

		case '1':
			return "快棋局";

		case '2':
			return "普通局";
	}
}


char * status_to_string(char status)
{
	switch(status)
	{
		case '0':
			return "黑胜";

		case '1':
			return "白胜";
	}
}


// void swap_itoa(int num, char *str)
// {
// 	char temp[5];

// 	itoa(num, temp, PERSON_LEN);
// 	strcpy(str, temp);

// 	return;
// }


int insert_club(const char *clubname, 
	const char *coach)
{
	Club *p1 = NULL;
	
	p1 = head;
	head = (Club *)malloc(sizeof(Club));
	debug("%s: %d", clubname, strlen(clubname));
	strcpy(head -> name, clubname);
	strcpy(head -> coach, coach);
	head -> sitepoint = 0;
	head -> gamepoint = 0;
	head -> player = NULL;
	head -> next = p1;

	return 0;
}


void insert_club_with_input()
{
	char clubname[CLUBNAME_LEN];
	char coach[PERSON_LEN];

	printf("clubname coach:");
	scanf("%s %s", clubname, coach);
	debug("%s %s", clubname, coach);
	getchar();
	insert_club(clubname, coach);

	return;
}


int show_clubs()
{
	Club *p1 = NULL;

	printf("%-15s %-10s %-3s %-3s\n", 
		"俱乐部", "教练", "场分", "局分");
	p1 = head;
	while(p1)
	{
		printf("%-15s %-10s %3d %3d\n", 
			p1 -> name, p1 -> coach, 
			p1 -> sitepoint, p1 -> gamepoint);
		p1 = p1 -> next;
	}

	return 0;
}


Club * query_club(const char *data, int flag)
{
	Club *p1;
	int flag_found = 0;

	p1 = head;
	//query by clubname
	if (flag == 1)
	{
		while(p1)
		{
			if (!strcmp(p1 -> name, data))
			{
				flag_found = 1;
				break;
			}
			p1 = p1 -> next;
		}		
	}
	else if (flag == 0)
	{
		while(p1)
		{
			if (!strcmp(p1 -> coach, data))
			{
				flag_found = 1;
				break;
			}
			p1 = p1 -> next;
		}
	}

	return (flag_found ? p1 : NULL);
}


Club * query_club_by_name(const char *name)
{
	return query_club(name, 1);
}


Club * query_club_by_coach(const char *coach)
{
	return query_club(coach, 0);
}


/**
 * get substring from users for query
 */
void search_club()
{
	char clubname[CLUBNAME_LEN];

	printf("enter keword:\n");
	scanf("%s", clubname);
	getchar();

	query_club_by_substring(clubname);

	return;
}


/**
 * query clubs with keywird
 * @param substr keyword
 */	
int query_club_by_substring(const char *substr)
{
	Club *pc;
	int flag_found = 0;

	printf("俱乐部信息\n");
	for(pc = head; pc; pc = pc -> next)
	{
		if(strstr(pc -> name, substr) != NULL)
		{
			flag_found = 1;
			printf("%s %s %d %d\n", 
				pc -> name, pc -> coach,
				pc -> sitepoint, pc -> gamepoint);

		}
	}

	return flag_found;
}


int query_club_by_coach_substr(const char *substr)
{
	Club *pc;
	int flag_found = 0;

	for(pc = head; pc; pc = pc -> next)
	{
		if(strstr(pc -> coach, substr) != NULL)
		{
			flag_found = 1;
			break;
		}
	}

	return flag_found;
}


void select_club(const char *mode)
{
	Club *p1 = NULL;
	char data[20];

	printf("Please enter the %s of the club:\n",
	 mode);
	scanf("%s", data);
	debug("%s", data);
	getchar();
	if (!strcmp(mode, "name"))
	{
		p1 = query_club_by_name(data);

	}
	else if (!strcmp(mode, "coach"))
	{
		p1 = query_club_by_coach(data);
	}
	show_club(p1);

	return;
}


void show_club(Club * const p)
{
	Player *pp;
	if (p)
	{
		printf("俱乐部: %s\t教练: %s\t场分: "
			"%d\t局分: %d\n",
			p -> name, p -> coach, 
			p -> sitepoint, p -> gamepoint);
		printf("姓名\t段位\t参赛局数\t胜局数\n");
		for (pp = p -> player; pp; pp = pp -> next)
		{
			printf("%-10s %c %3d \t %3d\n",
				pp -> name, pp -> rank, 
				pp -> gamesum, pp -> victories);
		}
	}
	else
	{
		printf("not found\n");
	}
	return;
}


int insert_player(const char *clubname, 
				  const char *name, 
				  const char rank)
{
	Club *pc;
	Player *p1;

	pc = query_club_by_name(clubname);
	p1 = (Player *)malloc(sizeof(Player));
	strcpy(p1 -> clubname, clubname);
	strcpy(p1 -> name, name);
	p1 -> rank = rank;
	p1 -> gamesum = 0;
	p1 -> victories = 0;
	p1 -> club = pc;
	p1 -> game = NULL;
	p1 -> next = pc -> player;
	pc -> player = p1;

	return 0;
}

void insert_player_with_input()
{
	char clubname[20];
	char name[CLUBNAME_LEN];
	char rank;

	printf("enter the name of club:");
	scanf("%s", clubname);
	debug("%s", clubname);
	getchar();
	if (query_club_by_name(clubname))	
	{
		printf("input the info of player\n\
			name rank:");
		scanf("%s %c", name, &rank);
		debug("%s %c", name, rank);
		getchar();
		if (query_player_by_name(clubname, name))
		{
			printf("player exists!\n");	
		}
		else
		{
			insert_player(clubname, name, rank);
		}
	}
	else
	{
		printf("there is no %s in clubs!\n",
		 clubname);
	}

	return;
}

Player * query_player_by_name(const	char *clubname, 
							  const char *name)
{
	Club *pc;
	Player *p1;

	pc = query_club_by_name(clubname);
	p1 = pc ? pc -> player : NULL;
	while(p1)
	{
		if (!strcmp(p1 -> name, name))
		{
			return p1;
		}
		p1 = p1 -> next;
	}
	return p1;
}


int query_player_by_name_only(const char *player)
{
	Club *pc;
	Player *pp;

	for(pc = head; pc; pc = pc -> next)
	{
		for(pp = pc -> player; pp; pp = pp -> next)
		{
			if (!strcmp(pp -> name, player))
			{
				return 1;
			}
		}
	}

	return 0;
}


int query_player_by_club_and_victories(const char *clubname, int victories)
{
	Club *pc;
	Player *pp;

	pc = query_club_by_name(clubname);
	if(pc)
	{
		for(pp = pc -> player; pp; pp = pp -> next)
		{
			if (pp -> victories == victories)
			{
				return 1;
			}
		}
	}
	else
	{
		return 0;
	}

	return 0;
}


void select_players_by_name()
{
	char name[CLUBNAME_LEN];

	printf("enter the player's name:\n");
	scanf("%s", name);
	debug("%s", name);
	getchar();
	show_players_by_name(name);

	return;
}


void select_player_by_club_and_name()
{
	char clubname[CLUBNAME_LEN];
	char name[PERSON_LEN];

	printf("clubname name:\n");
	scanf("%s %s", clubname, name);
	debug("%s %s", clubname, name);
	getchar();
	show_player_by_club_and_name(clubname, name);

	return;
}


void select_players_by_club_and_victories()
{
	char clubname[PERSON_LEN];
	int victories;

	printf("clubname victories\n");
	scanf("%s %d", clubname, &victories);
	getchar();
	debug("%s %d", clubname, victories);

	show_players_by_club_victories(clubname, victories);

	return;
}


void show_player(Player *pp)
{
	Game *pg;

	printf("%-10s %-15s %-4s %-4s %4s\n",
		"姓名", "俱乐部", "段位", "局数", "胜场");
	printf("%-10s %-15s %c %3d %3d\n",
	pp -> name, pp -> clubname, 
	pp -> rank,
	pp -> gamesum, pp -> victories);
	printf("对局信息\n");
	printf("场次\t局别\t执黑\t执白\t"
		"黑队\t白队\t胜负\n");
	for(pg = pp -> game; pg; 
		pg = pg -> next)
	{
		printf("%-3d\t%c\t%s\t"
			   "%s\t%s\t%s\t%c\n", 
			pg -> id, pg -> tag, 
			pg -> black, pg -> white, 
			pg -> blackteam, 
			pg -> whiteteam, 
			pg -> status);
	}

	return;
}


void show_players_by_name(const char *name)
{
	Club *pc;
	Player *p1;
	Game *pg;
	int flag_found = 0;

	pc = head;
	while(pc)
	{
		p1 = pc -> player;
		while(p1)
		{
			if (!strcmp(p1 -> name, name))
			{
				flag_found = 1;
				show_player(p1);
			}
			p1 = p1 -> next;
		}
		pc = pc -> next;
	}
	if (!flag_found)
	{
		printf("not found\n");
	}

	return;
}


void show_player_by_club_and_name(
	const char *clubname, 
	const char *name)
{
	Club *pc;
	Player *p1;
	Game *pg;
	int flag_found = 0;

	pc = query_club_by_name(clubname);
	printf("%-10s %-15s %-4s %-4s %4s\n",
		"姓名", "俱乐部", "段位", "局数", "胜场");
	if (!pc)
	{
		printf("not found the club \"%s\"\n",
		 clubname);		
		return;
	}
	p1 = pc -> player;
	while(p1)
	{
		if (!strcmp(p1 -> name, name))
		{
			printf("%-10s %-15s %c %3d %3d\n",
				p1 -> name, p1 -> clubname,
			 	p1 -> rank,
				p1 -> gamesum, p1 -> victories);
			flag_found = 1;

			printf("对局信息\n");
			printf("场次\t局别\t执黑\t执白\t"
				"黑队\t白队\t胜负\n");
			for(pg = p1 -> game; pg; 
				pg = pg -> next)
			{
				printf("%-3d\t%c\t%s\t"
					   "%s\t%s\t%s\t%c\n", 
					pg -> id, pg -> tag, 
					pg -> black, pg -> white, 
					pg -> blackteam, 
					pg -> whiteteam, 
					pg -> status);
			}
		}
		p1 = p1 -> next;
	}
	if (!flag_found)
	{
		printf("not found %s in %s\n", 
			name, clubname);
	}
}


void show_players_by_club_victories(const char *clubname, 
	int victories)
{
	Club *pc;
	Player *pp;
	int flag_found = 0;

	pc = query_club_by_name(clubname);
	for(pp = pc -> player; pp; pp = pp -> next)
	{
		if (pp -> victories == victories)
		{
			int flag_found = 1;
			show_player(pp);
		}
	}
	if (!flag_found)
	{
		printf("not found!\n");
	}

	return;
}


void insert_game(const int id, 
				 const char tag,
				 const char *blackteam, 
				 const char *whiteteam,
				 const char *black, 
				 const char *white, 
				 const char status, 
				 Player *pp)
{
	check_gamepoint(id, tag, blackteam, whiteteam,
		black, white, status, pp);
	check_sitepoint(id, pp -> clubname);
	
	return;
}

void insert_game_with_input()
{
	int id;
	char tag;
	char blackteam[CLUBNAME_LEN];
	char whiteteam[CLUBNAME_LEN];
	char black[PERSON_LEN];
	char white[PERSON_LEN];
	char status;
	Player *p1;
	Player *p2;

	//场次 局别 执黑俱乐部 执白俱乐部 黑 白 胜负
	printf("id tag blackteam whiteteam " 
		   "black white status\n");
	scanf("%d %c %s %s %s %s %c", 
		&id, &tag, blackteam, whiteteam, 
		black, white, &status);
	getchar();
	debug("%d %c %s %s %s %s %c", 
		id, tag, blackteam, whiteteam, 
		black, white, status);
	if (check_game(blackteam, id, black, tag) || 
		check_game(whiteteam, id, white, tag))
	{
		printf("game record exists!\n");
	}
	else
	{
		if (p1 = query_player_by_name(blackteam, 
			black))
		{
		  if(p2 = query_player_by_name(whiteteam,
		   white))
			{
				insert_game(id, tag, 
					blackteam, whiteteam,	
					black, white, status, p1);
				insert_game(id, tag, 
					blackteam, whiteteam, 
					black, white, status, p2);		
			}	
			else
			{
				printf("no '%s' or '%s'\n", 
					whiteteam, white);
			}
		}
		else
		{
			printf("no '%s' or '%s'\n", 
				blackteam, black);
		}
	}
}


Game * query_game(const char *clubname, 
				  int id, 
				  const char *player)
{

	Player *pp;
	Game *p1 = NULL;
	
	pp = query_player_by_name(clubname, player);
	debug("query_game params: %s %s", 
		clubname, player);
	debug("player %s", pp -> name);
	if (pp)	
	{
		p1 = pp -> game;
		while(p1)
		{
			if (p1 -> id == id)
			{
				debug("id is the same!");
				return p1;
			}
			p1 = p1 -> next;
		}
	}

	return p1;
}


int query_game_before_delete(const int id, 
							const char *blackteam,
							const char *whiteteam,
							const char *black,
							const char *white)
{
	Club *pc;
	Player *pp;
	Game *pg;

	pg = query_game(blackteam, id, black);
	if(pg)
	{
		if(!strcmp(pg -> white, white) && !strcmp(pg -> whiteteam, whiteteam))
		{
			return 1;
		}
	}

	return 0;
}




/**
 * check the legality of user's input
 * @return          0:legal 1:illegal
 */
int check_game(const char *clubname, 
			   int id, 
			   const char *player, 
			   char tag)
{
	Club *pc;
	Player *pp = NULL;
	Game *pg = NULL;
	int prime = 0;
	int fast = 0;
	int common = 0;

	if (query_game(clubname, id, player))
	{
		return 1;
	}
	pc = query_club_by_name(clubname);
	if (pc)
	{
		for (pp = pc -> player; pp; pp = pp -> next)
		{
			for (pg = pp -> game; 
				pg; 
				pg = pg -> next)
			{
				if (pg -> id == id)
				{
					switch(pg -> tag)
					{
						case '0':
							prime ++;
							break;

						case '1':
							fast ++;
							break;

						case '2':
							common ++;
							break;
					}
				}
			}	
		}
		switch(tag)
		{
			case '0':
				return (prime ? 1 : 0);

			case '1':
				return (fast ? 1 : 0);

			case '2':
				return ((common < 2) ? 0 : 1);
		}
	}

	return 0;
}


void show_game(Game * pg)
{
	printf("场次 棋手 局别 黑手 白手 黑方 白方 胜负\n");
	printf("%d %s %c %s %s %s %s %c\n", 
		pg -> id, pg -> player -> name,
		pg -> tag, pg -> black, pg -> white,
		pg -> blackteam, pg -> whiteteam,
		pg -> status);
	return;
}


int query_game_by_id_and_player(int id, const char *player)
{
	Club *pc;
	Player *pp;
	Game *pg;
	
	for(pc = head; pc; pc = pc -> next)
	{
		for(pp = pc -> player; pp; pp = pp -> next)
		{
			if (!strcmp(pp -> name, player))
			{
				for(pg = pp -> game; pg; pg = pg -> next)
				{
					if (pg -> id == id)
					{
						return 1;
					}
				}
			}
		}
	}

	return 0;
}


int query_game_by_status(const char *player, int flag)
{
	Club *pc;
	Player *pp;
	Game *pg;

	for(pc = head; pc; pc = pc -> next)
	{
		for(pp = pc -> player; pp; pp = pp -> next)
		{
			if (!strcmp(pp -> name, player))
			{
				for(pg = pp -> game; pg; pg = pg -> next)
				{
					if (flag)
					{
						if (get_winner(pg -> player -> name,
							   pg -> black, pg -> white, pg -> status))
						{
							return 1;
						}	
					}
					else
					{
						if (!get_winner(pg -> player -> name,
							   pg -> black, pg -> white, pg -> status))
						{
							return 1;
						}	
					}
					
				}
			}
		}
	}

	return 0;
}


/**
 * get the result of a game for a player, 
 * win or lose, must check the existence 
 * of params before using
 * @param  pg     game
 * @param  player playername
 * @return        0: lose, 1:win
 */
int get_winner(const char *player,
			   const char *black, 
			   const char *white,
			   char status)
{
	int flag;

	switch(status)
	{
		case '0':
			flag = strcmp(black, player) ? 0 : 1;
			break;

		case '1':
			flag = strcmp(white, player) ? 0 : 1;
			break; 
	}

	return flag;
}



int check_gamepoint(const int id, const char tag,
	const char *blackteam, const char *whiteteam,
	const char *black, const char *white, 
	const char status, Player *pp)
{
	Game *p1;

	p1 = (Game *)malloc(sizeof(Game));
	p1 -> id = id;
	p1 -> tag = tag;
	strcpy(p1 -> black, black);
	strcpy(p1 -> white, white);
	strcpy(p1 -> blackteam, blackteam);
	strcpy(p1 -> whiteteam, whiteteam);
	p1 -> status = status;
	p1 -> player = pp;
	p1 -> next = pp -> game;

	pp -> gamesum += 1;
	pp -> game = p1;
	switch(status)
	{
		case '0':
			if (!strcmp(black, pp -> name))
			{
				pp -> victories += 1;
				pp -> club -> gamepoint += 2;
			}
			break;

		case '1':
			if (!strcmp(white, pp -> name))
			{
				pp -> victories += 1;
				pp -> club -> gamepoint += 2;
			}
	}

	return 0;
}


void analysis_gamepoint(const int id,
				   Player *pp)
{
	Game *pg;

	for(pg = pp -> game; 
		pg -> id != id; 
		pg = pg -> next);
	pp -> gamesum += 1;
	switch(pg -> status)
	{
		case '0':
			if (!strcmp(pg -> black, pp -> name))
			{
				pp -> victories += 1;
				pp -> club -> gamepoint += 2;
			}
			break;

		case '1':
			if (!strcmp(pg -> white, pp -> name))
			{
				pp -> victories += 1;
				pp -> club -> gamepoint += 2;
			}
	}


	return;
}


int check_sitepoint(int id, const char *clubname)
{
	Club *pc;
	Player *pp;
	Game *pg;
	List *p1 = NULL;
	List *p2;
	int gamesum = 0;
	int victories = 0;
	int flag_sitepoint = 0;

	pc = query_club_by_name(clubname);
	if (pc)
	{
		for(pp = pc -> player; pp; pp = pp -> next)
		{
			for(pg = pp -> game; pg; pg = pg -> next)
			{
				if (pg -> id == id)
				{
					gamesum ++;
					p2 = p1;
					p1=(List*)malloc(sizeof(List));
					p1 -> id = pg -> id;
					p1 -> tag = pg -> tag;
					strcpy(p1 -> black,pg -> black);
					strcpy(p1 -> white,pg -> white);
					strcpy(p1 -> player, 
						pg -> player -> name);
					p1 -> status = pg -> status;
					p1 -> next = p2;
				}
			}
		}
	}
	else
	{
		printf("not found %s\n", clubname);
		return 0;
	}

	if (gamesum == 4)
	{
		for (p2 = p1; p2; p2 = p2 -> next)
		{	
			if (get_winner(p2 -> player,
				p2 -> black,
				p2 -> white,
				p2 -> status))
			{
				victories ++;
			}
		}
		switch(victories)
		{
			case 4:
			case 3:
				pc -> sitepoint += 3;
				break;

			case 2:
				for(p2 = p1; p2; p2 = p2 -> next)
				{
					if (p2 -> tag == '0')
					{
						if (get_winner(p2 -> player,
							p2 -> black,
							p2 -> white,
							p2 -> status))
						{
							flag_sitepoint = 2;
						}
						else
						{
							flag_sitepoint = 1;
						}
					}
				}
				break;
		}
	}
	pc -> sitepoint += flag_sitepoint;

	free(p1);
	free(p2);
	p1 = NULL;
	p2 = NULL;
	return 0;
}


void update_coach_by_clubname()
{
	Club *pc;
	char clubname[CLUBNAME_LEN];
	char coach[PERSON_LEN];

	printf("enter the clubname:");
	scanf("%s", clubname);
	debug("%s", clubname);
	getchar();
	pc = query_club_by_name(clubname);
	if (pc)
	{
		printf("enter new coach:");
		scanf("%s", coach);
		debug("%s", coach);
		getchar();
		strcpy(pc -> coach, coach);
	}
	else
	{
		printf("no %s!\n", clubname);
	}

	return;
}


void update_clubname_of_player()
{
	Club *pc;
	Player *pp;
	char clubname[CLUBNAME_LEN];
	char player[PERSON_LEN];
	char newclubname[CLUBNAME_LEN];
	char rank;

	printf("clubname player:");
	scanf("%s %s", clubname, player);
	debug("%s %s", clubname, player);
	getchar();
	pp = query_player_by_name(clubname, player);
	if (pp)
	{
		printf("enter the club of %s:", player);
		scanf("%s", newclubname);
		debug("%s", newclubname);
		getchar();
		pc = query_club_by_name(newclubname);
		if (pc)
		{
			rank = pp -> rank;
			delete_player(clubname, player);
			insert_player(newclubname,
				player, rank);
			
		}
		else
		{
			printf("%s not exists!\n", newclubname);
		}

	}
	else
	{
		printf("record not exists!\n");
	}

	return;
}


void update_rank()
{
	Player *pp;
	char clubname[CLUBNAME_LEN];
	char player[PERSON_LEN];
	char rank;

	printf("clubname player\n");
	scanf("%s %s", clubname, player);
	debug("%s %s", clubname, player);
	getchar();
	pp = query_player_by_name(clubname, player);
	if (pp)
	{
		printf("new rank:");
		scanf("%c", &rank);
		debug("%c", rank);
		getchar();
		pp -> rank = rank;
	}
	else
	{
		printf("record not exists!\n");
	}

	return;
}


void update_game()
{
	int id;
	char blackteam[CLUBNAME_LEN];
	char whiteteam[CLUBNAME_LEN];
	char black[PERSON_LEN];
	char white[PERSON_LEN];

	printf("id blackteam whiteteam black white\n");
	scanf("%d %s %s %s %s",
		&id, blackteam, whiteteam, black, white);
	debug("%d %s %s %s %s",
		id, blackteam, whiteteam, black, white);
	if(query_game_before_delete(id, blackteam, whiteteam, black, white))
	{
		delete_game(id, blackteam, whiteteam, 
			black, white);
		printf("enter new record\n");
		insert_game_with_input();
	}
	else
	{
		printf("record not exists!\n");
	}
}


void delete_club(const char *clubname)
{
	Club *pc;
	Club *previous;
	Player *pp;

	pc = query_club_by_name(clubname);
	for(pp = pc -> player; pp; pp = pp -> next)
	{
		debug("delete_player :%s > %s", 
			pp -> clubname, pp -> name);
		delete_player(pp -> clubname, pp -> name);
	}
	if (head == pc)
	{
		head = head -> next;
		free(pc);
	}
	else
	{
		for(previous = head; 
			previous -> next != pc;
			previous = previous -> next);
		previous -> next = pc -> next;
		free(pc);
	}
	pc = NULL;
	// check_all_point();

	return;
}


void delete_club_with_input()
{
	char clubname[CLUBNAME_LEN];

	printf("enter clubname:");
	scanf("%s", clubname);
	debug("%s", clubname);
	getchar();
	if (query_club_by_name(clubname))
	{
		delete_club(clubname);
	}
	else
	{
		printf("%s not exists!\n", clubname);
	}

	return;
}


void delete_player(const char *clubname, 
				   const char *player)
{
	Player *pp;
	Player *previous;
	Club *pc;
	Game *pg;

	pc = query_club_by_name(clubname);
	pp = query_player_by_name(clubname, player);
	for(pg = pp -> game; pg; pg = pg -> next)
	{
		delete_game(pg -> id,
					pg -> blackteam,
					pg -> whiteteam,
					pg -> black,
					pg -> white);
	}
	debug("pc is %p", pc);
	if (pc -> player == pp)
	{
		debug("1");
		pc -> player = pp -> next;
		// free(pp);
	}
	else
	{
		debug("2");
		for (previous = pc -> player; 
			 previous -> next != pp ; 
			 previous = previous -> next);	
		previous -> next = pp -> next;
		// free(pp);
	}
	pp = NULL;
	debug(">>>>>>>>>>>>>>>>>>>");
	show_all();
	debug(">>>>>>>>>>>>>>>>>>>");

	//check_all_point();

	return;
}


void delete_player_with_input()
{
	char clubname[CLUBNAME_LEN];
	char player[PERSON_LEN];

	printf("clubname player:\n");
	scanf("%s %s", clubname, player);
	debug("%s %s", clubname, player);
	getchar();
	if (query_player_by_name(clubname, player))
	{
		delete_player(clubname, player);
	}
	else
	{
		printf("record not exists!\n");
	}

	return;
}


void delete_game_single(int id,
						const char *clubname,
						const char *player)
{
	Club *pc;
	Player *pp;
	Game *pg;
	Game *previous;

	pp = query_player_by_name(clubname, player);
	debug("delete_game_single param:");
	debug("%d %s %s", id, clubname, player);
	pg = query_game(clubname, id, player);
	debug("delete \t%s:%s", 
		pg -> player -> name, 
		pg -> player -> clubname);
	if(pp -> game == pg)
	{
		pp -> game = pg -> next;
		free(pg);
	}
	else
	{
		for(previous = pp -> game;
			previous -> next != pg;
			previous = previous -> next);
		previous -> next = pg -> next;
		free(pg);
	}
	pg = NULL;
	pc = pp -> club;
	pc -> gamepoint = 0;
	pc -> sitepoint = 0;
	for(pp = pc -> player; pp; pp = pp -> next)
	{
		pp -> victories = 0;
		pp -> gamesum = 0;
		for(pg = pp -> game; pg; pg = pg -> next)
		{
			analysis_gamepoint(pg -> id, pp);
			check_sitepoint(pg -> id, 
							pp -> club -> name);
		}
	}

	return;
}


void delete_game(int id,
				 const char *blackteam,
				 const char *whiteteam,
				 const char *black,
				 const char *white)
{
	char temp_clubname[CLUBNAME_LEN];
	char temp_player[PERSON_LEN];

	strcpy(temp_clubname, whiteteam);
	strcpy(temp_player, white);

	debug("id\tblackteam\twhiteteam\t"
		   "black\twhite\t");
	debug("%d %s %s %s %s", 
		id, blackteam, whiteteam, black, white);
	delete_game_single(id, blackteam, black);
	debug("before white is :%s", white);
	// delete_game_single(id, whiteteam, white);
	debug("temp_player: %s", temp_player);
	delete_game_single(id, whiteteam, temp_player);
	debug("delete_game param:"
		"%d %s %s", id, whiteteam, white);
	show_all();

	return;
}


void delete_game_with_input()
{
	int id;
	char blackteam[CLUBNAME_LEN];
	char whiteteam[CLUBNAME_LEN];
	char black[PERSON_LEN];
	char white[PERSON_LEN];

	printf("id blackteam black whiteteam white\n");
	scanf("%d %s %s %s %s", 
		&id, blackteam, black, whiteteam, white);
	debug("%d %s %s %s %s", 
		id, blackteam, black, whiteteam, white);
	getchar();
	if (query_game(blackteam, id, black) &&
		query_game(whiteteam, id, white))
	{
		delete_game(id, blackteam, whiteteam, 
			black, white);
	}
	else	
	{
		printf("record not exists!\n");
	}

	return;
}


int save()
{
	Club *pc;
	Player *pp;
	Game *pg;
	Club_t *pct;
	Player_t *ppt;
	Game_t *pgt;

	FILE *club_table, *player_table, *game_table;

	club_table = fopen("club", "w");
	player_table = fopen("player","w");
	game_table = fopen("game", "w");
	
	for(pc = head; pc; pc = pc -> next)
	{
		pct = (Club_t *)malloc(sizeof(Club_t));
		strcpy(pct -> name, pc -> name);
		strcpy(pct -> coach, pc -> coach);
		pct -> sitepoint = pc -> sitepoint;
		pct -> gamepoint = pc -> gamepoint;
		fwrite(pct, sizeof(Club_t), 1, club_table);
		for(pp = pc -> player; pp; pp = pp -> next)
		{
			ppt = (Player_t *)malloc(sizeof(Player_t));
			strcpy(ppt -> clubname, pp -> clubname);
			strcpy(ppt -> name, pp -> name);
			ppt -> rank = pp -> rank;
			ppt -> gamesum = pp -> gamesum;
			ppt -> victories = pp -> victories;
			fwrite(ppt, sizeof(Player_t), 1, player_table);
			for(pg = pp -> game; pg; pg = pg -> next)
			{
				pgt = (Game_t *)malloc(sizeof(Game_t));
				strcpy(pgt -> clubname,
						pg -> player -> clubname);
				strcpy(pgt -> player, pg -> player -> name);
				pgt -> id = pg -> id;
				pgt -> tag = pg -> tag;
				strcpy(pgt -> black, pg -> black);
				strcpy(pgt -> white, pg -> white);
				pgt -> status = pg -> status;
				strcpy(pgt -> blackteam, pg -> blackteam);
				strcpy(pgt -> whiteteam, pg -> whiteteam);
				debug("%s %s %d %c %s %s %c %s %s", 
					pgt -> clubname, pgt -> player,
					pgt -> id, pgt -> tag,
					pgt -> black, pgt -> white,
					pgt -> status, pgt -> blackteam,
					pgt -> whiteteam);
				fwrite(pgt, sizeof(Game_t), 1, game_table);
			}
		}
	}
	fclose(club_table);
	fclose(player_table);
	fclose(game_table);

	return 0;
}


int load()
{
	Club *pc;
	Player *pp;
	Game *pg;
	Player *p;
	Club_t *pct;
	Player_t *ppt;
	Game_t *pgt;
	int flag = 0;
	// int flag_open = 1;

	FILE *club_table, *player_table, *game_table;

	pct = (Club_t *)malloc(sizeof(Club_t));
	ppt = (Player_t *)malloc(sizeof(Player_t));
	pgt = (Game_t *)malloc(sizeof(Game_t));

	if(club_table = fopen("club", "r"))
	{
		head = NULL;
		while(!feof(club_table))
		{
			fread(pct, sizeof(Club_t), 1, club_table);
			if(!feof(club_table))
			{
				pc = head;
				head = (Club *)malloc(sizeof(Club));
				strcpy(head -> name, pct -> name);
				strcpy(head -> coach, pct -> coach);
				head -> sitepoint = pct -> sitepoint;
				head -> gamepoint = pct -> gamepoint;
				head -> player = NULL;
				head -> next = pc;
				flag = 1;		
			}
		}

		debug("flag club_table");
		fclose(club_table);
		debug("close club_table");
	}

	if(player_table = fopen("player", "r"))
	{
		while(!feof(player_table))
		{
			debug("here");
			debug("%p", ppt);
			fread(ppt, sizeof(Player_t), 1, player_table);
			debug("if start");
			if(!feof(player_table))
			{
				debug("begin");
				pc = query_club_by_name(ppt -> clubname);
				debug("flag");
				pp = (Player *)malloc(sizeof(Player));
				strcpy(pp -> clubname, ppt -> clubname);
				strcpy(pp -> name, ppt -> name);
				pp -> rank = ppt -> rank;
				pp -> gamesum = ppt -> gamesum;
				pp -> victories = ppt -> victories;
				pp -> club = pc;
				pp -> game = NULL;
				pp -> next = pc -> player;
				pc -> player = pp;
			}
			debug("end!");
		}
		fclose(player_table);
		debug("close player_table");
	}
	
	if(game_table = fopen("game", "r"))
	{
		debug("load game...");
		while(!feof(game_table))
		{
			debug("loop");
			fread(pgt, sizeof(Game_t), 1, game_table);
			if(!feof(game_table))
			{
				debug("start!");
				pp = query_player_by_name(pgt -> clubname, pgt -> player);
				pg = (Game *)malloc(sizeof(Game));
				pg -> id = pgt -> id;
				pg -> tag = pgt -> tag;
				strcpy(pg -> black, pgt -> black);
				strcpy(pg -> white, pgt -> white);
				pg -> status = pgt -> status;
				strcpy(pg -> blackteam, pgt -> blackteam);
				strcpy(pg -> whiteteam, pgt -> whiteteam);
				pg -> player = pp;
				pg -> next = pp -> game;
				pp -> game = pg;	
			}
		}
		debug("loop end!");
		fclose(game_table);
		debug("close game_table");
	}

	return 0;
}


void show_all()
{
	Club *pc;
	Player *pp;
	Game *pg;

	for(pc = head; pc; pc = pc -> next)
	{
		printf("俱乐部\n");
		printf("%s %s %d %d\n", pc -> name, 
			pc -> coach, pc -> sitepoint, 
			pc -> gamepoint);
		for(pp = pc -> player; pp; pp = pp -> next)
		{
			printf("棋手\n");
			printf("%s %c %d %d\n", pp -> name,
				pp -> rank,
				pp -> gamesum, pp -> victories);
			for(pg = pp -> game;pg;pg = pg -> next)
			{
				printf("对局\n");
				printf("%d %s %s %s %s %c\n", 
					pg -> id, pg -> black, 
					pg -> white, pg -> blackteam, 
					pg -> whiteteam, pg -> status);
			}
		}
	}

	return;
}

